package gapidt.nlp;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;

import edu.smu.tspell.wordnet.Synset;
import edu.smu.tspell.wordnet.SynsetType;
import edu.smu.tspell.wordnet.WordNetDatabase;

public class SemanticUtils {
	
	private static Logger logger = Logger.getLogger(SemanticUtils.class);
	
	// Terminaciones de sustantivos plurales regulares
	public static String[][] regularNounsPluralEndings = { 
		{"(.*)zes$", "$1z"},
		{"(.*([^aeiou\\W]))ies$", "$1y"},
		{"(.*)ves$", "$1f"},
		{"(.*(ch|x|s|sh|z|[^aeiou\\W]o))es$", "$1"},
		{"(.*)fe$", "$1f"},
		{"(.*)s$", "$1"}
	};
	
	/**
	 * Converts a camelCased notation string into a human redable phrase
	 * @param camelCased
	 * @return
	 */
	public static String camelCase2Human(String camelCased){
		return camelCased.replaceAll("([0-9]|-|_)+", " ").replaceAll("([a-z0-9])([A-Z0-9])", "$1 $2").replaceAll("([A-Z0-9]+)([A-Z0-9][a-z0-9]{2,50})", "$1 $2").replaceAll("\b+", " ");
	}
	
	public static String camelCase2Underscores(String camelCased){
		return camelCased.replaceAll("([a-z0-9])([A-Z0-9])", "$1_$2").replaceAll("([A-Z0-9]+)([A-Z0-9][a-z0-9]{2,50})", "$1_$2").trim();
	}
	
	public static List<String> getNonVerbs(String sentence){
		
		WordNetDatabase database = WordNetDatabase.getFileInstance();
		
		List<String> nonVerbs = new ArrayList<String>();
		
		List<Word> relevantWords = SemanticParser.getRelevantWords(sentence);
		
		for(Word word : relevantWords){
			if(!word.getType().startsWith("VB")){
				nonVerbs.add(stemNoun(word.getWord()));
			}
		}
		
		return nonVerbs;
	}
	
	public static String stemNoun(String noun){
		noun = noun.toLowerCase();
		if(noun.length() >= 2){
			for(String[] ending : regularNounsPluralEndings){
				if(noun.matches(ending[0])){
					noun = noun.replaceAll(ending[0], ending[1]);
					break;
				}
			}
		}
		return noun;
	}
	
	public static String getFirstSentence(String text, int maxWords){
		
		if(text == null)
			return "";
		
		// Las llaves causan problemas al tokenizer
		text = text.replaceAll("[\\}\\{@]", "");
		
		String[] parts = text.split("[\\.]");
		
		int wordCount = 0;
		String sentence = "";
		for(String part : parts){
			StringTokenizer st = new StringTokenizer(part, " \t\r\n.,;");
			List<String> tokens = new ArrayList<String>();
			while(st.hasMoreTokens()){
				tokens.add(st.nextToken());
			}
			int tokenCount = tokens.size();
			if(wordCount < maxWords){
				
				String s = part;
				if(wordCount + tokenCount > maxWords){
					s = "";
					int w = 0;
					for(String t : tokens){
						if(wordCount + w < maxWords){
							s += t + " ";
							w++;
						}else{
							break;
						}
					}
					wordCount += w;
				}else{
					wordCount += tokenCount;
				}
				
				sentence += s.trim() + (s.indexOf(".") > 0 ? "" : ".") + " ";
			}
			
		}
		
		return sentence.trim();
	}
}
